/*
 * @Author: 无聊 7989314+Hwz1997@user.noreply.gitee.com
 * @Date: 2025-10-28 14:30:58
 * @LastEditors: 无聊 7989314+Hwz1997@user.noreply.gitee.com
 * @LastEditTime: 2025-10-28 14:54:22
 * @FilePath: \keep_motion_api\controllers\admin\permission\log.controller.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const { Log } = require('@/models/permission');

// 查询字典类型
const getLogs = async (req, res) => {
  try {
    const { admin_name, pageNum, pageSize } = req.query;
    const offset = (pageNum - 1) * pageSize;
    const limit = pageSize;
    const {rows, total} = await Log.getLogs(admin_name, offset, limit);
    res.status(200).send({rows, total});
  } catch (error) {
    res.status(500).json({ message: '查询操作日志失败', error });
  }
}

// 删除字典类型
const deleteLog = async (req, res) => {
  try {
    const { id } = req.params;
    const log = await Log.deleteLog(id);
    res.status(200).json({ message: '删除操作日志成功', log });
  } catch (error) {
    res.status(500).json({ message: '删除操作日志失败', error });
  }
}

// 批量导出
const exportLogs = async (req, res) => {
  try {
    const { admin_name } = req.query;
    const { buffer, filename } = await Log.exportLogs( admin_name );
    res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    res.setHeader('Content-Disposition', `attachment; filename=${encodeURIComponent(filename)}`);
    res.status(200).send(buffer);
  } catch (error) {
    res.status(500).json({ message: '导出操作日志失败', error });
  }
}



module.exports = {
  getLogs,
  deleteLog,
  exportLogs
}
